package org.wu.framework.lazy.orm.core.persistence.reverse.lazy.vue;

import org.wu.framework.core.utils.FileUtil;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.core.config.ReverseEngineering;
import org.wu.framework.lazy.orm.core.persistence.reverse.ReverseClassLazyTableEndpoint;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.DefaultAbstractJavaReverseEngineeringMethod;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 默认的抽象生成vue类
 */
public abstract class DefaultAbstractVue extends DefaultAbstractJavaReverseEngineeringMethod {
    protected DefaultAbstractVue(ReverseClassLazyTableEndpoint reverseClassLazyTableEndpoint, ReverseEngineering reverseEngineering) {
        super(reverseClassLazyTableEndpoint, reverseEngineering);
    }


    /**
     * 创建 class 上下文代码
     *
     * @return
     */
    @Override
    public String createJavaContextCode() {
        //  添加 import class
        initImportClassName();
        //  添加 class 注解代码片段
        initClassAnnotationPart();

        //  添加 class name、描述
        initClassNamePart();

        // 添加 字段片段
        initClassFieldPart();

        // 添加 方法片段
        initClassMethodPart();
        return doCreateVueContextCode();
    }


    /**
     * 创建Vue 文件内容
     *
     * @return Vue文件内容
     */
    protected abstract String doCreateVueContextCode();

    /**
     * 获取文件后缀
     *
     * @return 文件后缀
     */
    @Override
    protected String getFileSuffix() {
        return ".vue";
    }

    /**
     * 获取resources下文件数据
     * @param fileName  resources下文件 如：vue/Edit.vue
     * @return
     */
    public String getFormatContent(String fileName){
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName);

        if(ObjectUtils.isEmpty(resourceAsStream)){
            throw new RuntimeException("无法找到文件:"+fileName);
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream));
        return FileUtil.readFileContent(bufferedReader);
    }

}
